package chen.web.util;

public class SubString {

    private static boolean isLetter(char c) {
        int k = 0x80;
        return c / k == 0 ? true : false;
    }
    
    public static String subString(String str, int n){
    	if(str == null){
    		return "";
    	}
    	if(n <= 0){
    		throw new IllegalArgumentException();
    	}
    	if(str.length() < n/2){
    		return str;
    	}
    	int length = 0;
    	int index = -1;
    	for(int i=0; i<str.length(); i++){
    		char ch = str.charAt(i);
    		++ length;
    		if(!isLetter(ch)){
    			++ length;
    		}
     		if(length > n){
    			index = i;
    			break;
    		}
    	}
    	if(index == -1){
    		return str;
    	} else{
    		return str.substring(0,index);	
    	}
    }
}
